/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2015-02-05 11:46:56
 * @filename : static/site/nff/controller/readEmail.js
 * @version : 
 * @description : 
 */

define(function(require, exports, module){
    var ReadEmailView, nav='readEmail', emailType, emailName,eId,
        mId = 'readEmail',reRender = true,menuName = 'readEmail',
        tpl = require('{view}readEmail.tpl'),
        panel = require('{component}panel/zepto.panel'),
        imglazyload = require('{component}imglazyload/zepto.imglazyload'),
        tplN = require('{view}userNav.tpl');

    ReadEmailView = Backbone.View.extend({

        el: '',

        template: _.template(tpl),
        templateN: _.template(tplN),

        initialize: function () {},

        render: function (obj) {
            if (!share.checkPermission(true)){return;};
            share.reRender(reRender,mId);
            var arr = obj.val.split('##');
            emailType = arr[1];
            emailName = arr[2];
            eId = arr[0];
            if ($('#'+mId).length < 1) { 
                $('body').append(this.template());
                this.bindEvt();            
            } else {
                $('#'+mId).css('display','block');
            }
            share.panel(panel,this.templateN,mId,menuName);
            this.getAjaxEmail({message_id:arr[0],subfolder:arr[1]}); 
        },

        bindEvt : function() {
            var tthis = this,dom = $('#'+mId);     
            
            dom.find('.back').on('tap',function(e){
                e.stopPropagation();
                window.location.href = '#emailList/whole/custom'+emailType+'##'+emailName;
            }); 

            dom.find('.emailHead_right_line').on('tap',function() {
                if($(this).attr('state') == 'true') {
                  $(this).css({'white-space' : 'nowrap'}).attr('state','false');
                } else {
                  $(this).css({'white-space' : 'normal'}).attr('state','true');
                }
            });

            dom.find('.reply').on('tap',function(e){
                e.stopPropagation();
                var username = 'Lyle';
                window.location.href = '#reply/whole/custom'+JSON.stringify({type:'addUser',userName:username,eId:eId,emailType:emailType});
            });

            dom.find('.prev').on('tap',function(){
                var eid = $(this).attr('eid'),
                    type = $(this).attr('folder'); 
                if (eid && type) {
                    window.location.href = '#readEmail/whole/custom'+eid+'##'+type+'##'+emailName; 
                }
            });

            dom.find('.next').on('tap',function(){
                var eid = $(this).attr('eid'),
                    type = $(this).attr('folder'); 
                if (eid && type) {
                    window.location.href = '#readEmail/whole/custom'+eid+'##'+type+'##'+emailName; 
                }
            });

            dom.find('.emailHead_left').on('tap',function(e) {
                var uid = $(e.target).closest('label').attr('uid'),
                    userName = $(e.target).closest('label').attr('username');
                if (uid) {
                    if ($(window).height() < 640) {
                        if (panel.info.state == 'hidden') {
                            window.location.href = '#userProfile/whole/custom'+uid+'&&'+userName; 
                        }
                    } else {
                        window.location.href = '#userProfile/whole/custom'+uid+'&&'+userName; 
                    }
                }
            });

        },

        getAjaxEmail : function(data) {
            var tthis = this , dom = $('#'+mId).find('.body_all');
            var obj = {
                url : seajs.data.vars.apiAccessUrl+'wm_display_email',
                type : 'POST',
                data : data,
                success : function(data) {
                    if (data.ret) {
                        var Img = seajs.data.vars.resourceUrl+'img/';
                        dom.find('.emailHead_right_name').html(data.from);
                        dom.find('.emailHead_right_line').html(data.subject);
                        dom.find('.emailHead_right_time').html(tthis.getMonthDayYearDate(data.date));
                        dom.find('.emailBody').html(data.content);
                        dom.find('a').attr('target','_blank');
                        var icon = '',uid= '';
                        var username = data.from.toLowerCase();
                        var userInfo = data.info[data.from.toLowerCase()];
                        userInfo = userInfo ? userInfo : {};
                        if (parseInt(userInfo.numberofpictures) > 0 && parseInt(userInfo.profile_hidden) != 1) {
                            icon = userInfo.icon;
                            uid = userInfo.usr_id;
                        } else {
                            if (userInfo.gender == 1) {
                                icon = Img + 'icon_male.png';
                            } else if (userInfo.gender == 2) {
                                icon = Img + 'icon_remale.png';
                            } else {
                                icon = Img + 'icon_married.png';
                            }
                        }
                        dom.find('.emailHead_left').html('<label uid="'+uid+'" username="'+username+'"><img src="' + icon + '" height="50"></label>');
                        tthis.emailPrevNext(data);
                    }
                },
                error : function(xhr, errorType, error) {},
                complete : function(xhr, status) {
                    share.pageLoad(false,$('body'));
                }
            };
            share.pageLoad(true,$('body'));
            $.ajax(share.ajaxControl(obj));
        },

        emailPrevNext : function(data) {

            if (data.next_msg_folder && data.next_msg_id) {
                $('#'+mId).find('.next').attr({
                    folder : data.next_msg_folder,
                    eid : data.next_msg_id
                });   
            } else {
                $('#'+mId).find('.next').css('visibility','hidden');
            }

            if (data.pre_msg_folder && data.pre_msg_id) {
                $('#'+mId).find('.prev').attr({
                    folder : data.pre_msg_folder,
                    eid : data.pre_msg_id
                }) 
            } else {
                $('#'+mId).find('.prev').css('visibility','hidden');
            }
        },

        getMonthDayYearDate : function(str) {
          var arr = new Date(parseInt(str)*1000).toString().split(' ');
          return arr[1]+' '+arr[2]+', '+arr[3];
        },

    });
    module.exports = ReadEmailView;
});
